package com.roa.utilidad.sql;

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


/**
 * Clase generica de conexion
 * 
 * @author jlrojas
 *
 */
public class ConexionGenerica {
	
	private static ConexionGenerica conexionGenerica;
	private Connection conexion; 
	
	private ConexionGenerica(){
		 
		try{
			
			Context ctx1 = new InitialContext();
			DataSource ds = (DataSource) ctx1.lookup("java:/sqlserver34");

			this.setConexion(ds.getConnection());
			
		}catch(SQLException e){
			System.out.println(e.getStackTrace());
		}catch (NamingException e) {
			// TODO: handle exception
			System.out.println(e.getStackTrace());
		}
	}
	
	

	public Connection getConexion() {
		return conexion;
	}

	public void setConexion(Connection conexion) {
		this.conexion = conexion;
	}

	/**
	 * 
	 * @return ConexionGenerica 
	 */
	public static ConexionGenerica getConexionGenerica(){
		
		if(conexionGenerica == null){
			conexionGenerica = new ConexionGenerica();
		}
		
		return conexionGenerica;
	}
	
	public Object clone() throws CloneNotSupportedException{
		throw new CloneNotSupportedException();
	}
	
}
